package com.cks.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cks.eduservice.entity.EduChapter;
import com.cks.eduservice.entity.EduVideo;
import com.cks.eduservice.entity.chapter.ChapterVo;
import com.cks.eduservice.entity.chapter.VideoVo;
import com.cks.eduservice.mapper.EduChapterMapper;
import com.cks.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cks.eduservice.service.EduVideoService;
import com.cks.servicebase.exceptionhandler.CksException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-18
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;


    @Override
    public List<ChapterVo> getChapterVideoBycourseId(String courseId) {

     //   System.out.println(courseId);
        //根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_Id", courseId);
     //   System.out.println(wrapper);
        List<EduChapter> eduChapters = baseMapper.selectList(wrapper);
       // System.out.println(eduChapters);
        //根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("course_Id", courseId);
        List<EduVideo> list = eduVideoService.list(eduVideoQueryWrapper);
        //创建list集合用于最终封装集合
        ArrayList<ChapterVo> chapterVos = new ArrayList<>();
        //遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter eduChapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            chapterVos.add(chapterVo);
           //创建 集合用于封装章节的小节
            ArrayList<VideoVo> videoVos = new ArrayList<>();

            //遍历查询小节list集合进行封装
            for (int j = 0; j < list.size();j++) {
                //得到每个小节
                EduVideo eduVideo = list.get(j);
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);

        }
        return chapterVos;
    }
//删除章节的方法
    @Override
    public boolean delChapterbyId(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = eduVideoService.count(wrapper);
        if(count>0){
          throw new CksException(2001,"请先删除小节");
        }else {
            int result = baseMapper.deleteById(chapterId);
             return result>0;
        }

    }
//根据课程id删除章节
    @Override
    public void removeChapterById(String id) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_Id",id);
        baseMapper.delete(wrapper);
    }
}
